package com.sr.vo.req;

import com.sr.pojo.WType;
import com.sr.util.ToolUtil;
import com.sr.vo.BaseForm;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.Date;

/**
 * @author xiaochi
 * @date 2022/4/30 9:30
 * @desc TypeReqVo
 */
@Data
@ApiModel("资源类型实体")
@EqualsAndHashCode(callSuper = true)
public class TypeReqVo extends BaseForm<WType> {

    @ApiModelProperty("资源类型Id，更新时必须")
    @Size(min = 19,max = 19,message = "id长度不正确",groups = Update.class)
    @NotBlank(message = "id不能为空",groups = Update.class)
    private String id;

    @ApiModelProperty("资源名称")
    @NotBlank(message = "名称不能为空",groups = {Create.class,Update.class})
    @Length(min = 1,max = 32,message = "名称长度未1 ~ 32位",groups = {Create.class,Update.class})
    private String name;

    @ApiModelProperty("资源名称描述")
    @Length(max = 64,message = "描述不能超过64个字符",groups = {Create.class,Update.class})
    private String desc;

    @ApiModelProperty("资源类型父级Id")
    @Size(min = 19,max = 19,message = "类型父id不正确")
    private String parentId;

    @Override
    public WType buildEntity() {
        WType wType = new WType();
        BeanUtils.copyProperties(this,wType);
        Date date = new Date();
        if (StringUtils.isEmpty(wType.getId())){
            wType.setId(ToolUtil.nextId());
            wType.setCreateTime(date);
            wType.setUpdateTime(date);
        }else {
            wType.setUpdateTime(date);
        }
        return wType;
    }
}
